package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.text.TextUtils
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.OrganizationActivity
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.server.UserRoleServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.SocialOrganAdapter
import cc.vv.btpartyjob.partymanager.adapter.SocialOrganDynamicAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.SheQuPartyDetailActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.resp.CommunityResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.party.beans.vo.SocietyOrgDynamicVO
import cc.vv.party.beans.vo.SocietyOrgVO

/**
 * 社区大党委详情
 * Created by Chen.qi on 2018/10/25.
 */
class SheQuPartyDetailActivity : BaseActivityMVP<SheQuPartyDetailActivityDelegate>() {
    /**
     * 社会组织adapter
     */
    private var mAdapter: SocialOrganAdapter? = null
    /**
     * 社会组织动态adapter
     */
    private var mAdapterDynamic: SocialOrganDynamicAdapter? = null

    /**
     * 社区ID
     */
    private var communityId: String? = null

    /**
     * 社区ID
     */
    private var orgName: String? = null

    /**
     * 社会组织列表
     */
    private var socList: ArrayList<SocietyOrgVO>? = null

    /**
     * 社会组织动态列表
     */
    private var socDynamicList: ArrayList<SocietyOrgDynamicVO>? = null


    override fun initData() {
        socList = ArrayList()
        socDynamicList = ArrayList()
        val userInfo = UserInfoManageUtil.getUserInfo()
        val userOrgList = userInfo?.userOrgList
        if (null != userOrgList) {
            for (i in userOrgList.indices) {
                if (userOrgList[i].type == 2) {
                    communityId = userOrgList[i].id
                    break
                }
            }
        }
        if (!TextUtils.isEmpty(communityId)) {
            PartyManagerServer.getSheQuZuZhiHttp(communityId!!, true)
        }
        mAdapter = SocialOrganAdapter(socList, this)
        viewDelegate?.setAdapterData(mAdapter!!)

        mAdapterDynamic = SocialOrganDynamicAdapter(socDynamicList, this)
        viewDelegate?.setAdapterDataDynamic(mAdapterDynamic!!)

        viewDelegate?.getSHZZ()?.setOnItemClickListener { parent, view, position, id ->
            //社会组织item
            intent.setClass(this, SheHuiZuZhiDetailActivity::class.java)
            intent.putExtra("id", socList?.get(position)?.id)
            startActivity(intent)
        }
        viewDelegate?.getDynamic()?.setOnItemClickListener { parent, view, position, id ->
            //社会组织动态item
            intent.setClass(this, SheHuiZuZhiDynamicDetailActivity::class.java)
            intent.putExtra("id", socDynamicList?.get(position)?.id)
            startActivity(intent)
        }



        viewDelegate?.setListViewFocusFalse()
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent(this@SheQuPartyDetailActivity, OrganizationActivity::class.java)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 2)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_CHOOSE, 2)
                startActivityForResult(intent, 100)
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //社会组织
            viewDelegate?.setSocialSelected(true)

        }, R.id.tv_asPd_SocialOrganization)
        viewDelegate?.setOnClickListener(View.OnClickListener {
            //社会组织动态
            viewDelegate?.setSocialSelected(false)
        }, R.id.tv_asPd_SocialOrganizationDynamic)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //网格
            val intent = Intent(this, WangGeListActivity::class.java)
            intent.putExtra("communityId", communityId)
            startActivity(intent)
        }, R.id.iv_asPd_wangGeAll)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //单位
            val intent = Intent(this, QiShiYeDanWeiActivity::class.java)
            intent.putExtra("communityId", communityId)
            startActivity(intent)
        }, R.id.iv_asPd_enterpriseAll)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //社会组织
            val intent = Intent(this, ZuZhiDynamicActivity::class.java)
            intent.putExtra("communityId", communityId)
            startActivity(intent)
        }, R.id.iv_asPd_socialOrganAll)

    }

    override fun getDelegateClass(): Class<SheQuPartyDetailActivityDelegate> {
        return SheQuPartyDetailActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is CommunityResponseObj -> {
                val zuZhiObj = obj.data ?: return
                viewDelegate?.setShowNoData(false)
                viewDelegate?.setTopBarTitle(zuZhiObj.title)

                if (!TextUtils.isEmpty(zuZhiObj.committeeIntro) || !TextUtils.isEmpty(zuZhiObj.name)) {
                    viewDelegate?.showWebData(true)
                } else {
                    viewDelegate?.showWebData(false)
                }
                viewDelegate?.setWebViewContent(zuZhiObj.committeeIntro)
                if (UserRoleServer.getAreaRole() || UserRoleServer.getStreetRole()
                        || UserRoleServer.getSheQuRole()
                        || UserRoleServer.getSheQuDaDangWeiRole()) {
                    viewDelegate?.setPerInfo(zuZhiObj.name, zuZhiObj.mobile, true)
                } else {
                    viewDelegate?.setPerInfo(zuZhiObj.name, zuZhiObj.mobile, false)

                }

                val gridList = zuZhiObj.gridList
                if (null != gridList && gridList.size > 0) {
                    viewDelegate?.setShowGrid(true)
                    viewDelegate?.setSheQuPartyWangGe(gridList)
                } else {
                    viewDelegate?.setShowGrid(false)
                }

                val unitList = zuZhiObj.unitList
                if (null != unitList && unitList.size > 0) {
                    viewDelegate?.setShowQiShiYeDanWei(true)
                    viewDelegate?.setEnterPriseData(unitList)
                } else {
                    viewDelegate?.setShowQiShiYeDanWei(false)
                }

                val societyOrgList = zuZhiObj.societyOrgList
                val societyOrgDynamicList = zuZhiObj.societyOrgDynamicList

                if ((null != societyOrgList && societyOrgList.size > 0) ||
                        (null != societyOrgDynamicList && societyOrgDynamicList.size > 0)) {
                    viewDelegate?.setShowSheHuiZuZhi(true)

                } else {
                    viewDelegate?.setShowSheHuiZuZhi(false)
                }

                socList!!.clear()
                socDynamicList!!.clear()

                if (null != societyOrgList) {
                    viewDelegate?.setSocialSelected(true)
                    socList!!.addAll(societyOrgList)
                    mAdapter?.notifyDataSetChanged()
                }
                if (null != societyOrgDynamicList) {
                    socDynamicList!!.addAll(societyOrgDynamicList)
                    mAdapterDynamic?.notifyDataSetChanged()
                }

            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        when (obj?.statusCode) {
            100001 -> {
                viewDelegate?.setWebViewContent("")
                viewDelegate?.setPerInfo("", "", false)
                viewDelegate?.setShowGrid(false)
                viewDelegate?.setShowQiShiYeDanWei(false)
                viewDelegate?.setShowSheHuiZuZhi(false)
                viewDelegate?.showWebData(false)
                viewDelegate?.setShowNoData(true)
            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            communityId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            orgName = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
            viewDelegate?.setTopBarTitle(orgName + "大党委")
            PartyManagerServer.getSheQuZuZhiHttp(communityId!!, true)
        }
    }
}